<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\SubjectSortStore;
use App\Subject;
use App\SubjectPower;
use App\SubjectSort;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class SubjectSortController extends Controller
{
    /**
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function index(Request $request)
    {
        $user_id=Auth::id();
        $categories = SubjectSort::all();
        foreach ($categories as $v){
            $power=SubjectPower::where('user_id', $user_id)->where('status', 0)->where('subject_sort_id', $v->id)->first();
            $v->is_power=0;//还没购买
            if($power){
                $v->is_power=1;//已购买
            }
        }
        return $categories;
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(SubjectSortStore $request)
    {
//        SubjectSort::create($request->all());
//        return response(['message' => '添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\SubjectSort $subjectSort
     * @return \Illuminate\Http\Response
     */
    public function show(SubjectSort $subjectSort)
    {
        //
    }

    /**
     * @param SubjectSortStore $request
     * @param SubjectSort $subjectSort
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function edit(Request $request, SubjectSort $subjectSort)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\SubjectSort $subjectSort
     * @return \Illuminate\Http\Response
     */
    public function update(SubjectSortStore $request, SubjectSort $subjectSort)
    {
        $subjectSort->update($request->only(['name', 'introduce','price']));
        return response(['message' => '修改成功']);
    }

    /**
     * @param SubjectSort $subjectSort
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function destroy(SubjectSort $subjectSort)
    {
//        if (Subject::where('sort', $subjectSort->id)->first()) {
//            return response(['message' => '该分类下存在题目，不能删除'], 400);
//        }
//        $subjectSort->delete();
//        return response(['message' => '删除成功']);
    }
}
